package com.yhp.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.comm.util.DateUtils;
import com.system.common.mapper.BisBagProjectMapper;
import com.system.common.mapper.BisNurseBagMapper;
import com.system.common.mapper.BisNurseProjectMapper;
import com.system.common.pojo.BisBagProject;
import com.system.common.pojo.BisBagProjectExample;
import com.system.common.pojo.BisNurseBag;
import com.system.common.pojo.BisNurseBagExample;
import com.system.common.pojo.BisNurseBagExample.Criteria;
import com.system.common.pojo.BisNurseProject;
import com.system.common.pojo.BisNurseProjectExample;

@Service
public class BisNurseBagServiceImpl implements BisNurseBagService {
	@Autowired
	private BisNurseBagMapper bagMapper;
	@Autowired
	private BisBagProjectMapper bagProjectMapper;
	@Autowired
	private BisNurseProjectMapper projectMapper;

	/**
	 * 礼包>>>>>>列表显示所有+综合查询
	 */
	@Override
	public List<BisNurseBag> selectByExample(Map<String, Object> map) {
		String bagId=(String)map.get("bagId");
		String bagName=(String)map.get("bagName");
		String status=(String)map.get("status");
		BisNurseBagExample example = new BisNurseBagExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(bagId)) {
			criteria.andBagIdLike("%"+bagId+"%");
		}
		if (StringUtils.isNotBlank(bagName)) {
			criteria.andBagNameLike("%"+bagName+"%");
		}
		if (StringUtils.isNotBlank(status)) {
			criteria.andStatusEqualTo(status);
		}
		List<BisNurseBag> selectByExample = this.bagMapper.selectByExample(example);
		List<BisNurseBag> bagList=new ArrayList<>();
		for (BisNurseBag bisNurseBag : selectByExample) {
			if (bisNurseBag.getIsDel().equals("0")) {
				bagList.add(bisNurseBag);
			}
		}
		return bagList;
	}

	/**
	 * 礼包>>>>>>详情
	 */
	@Override
	public BisNurseBag selectByPrimaryKey(String bagId) {
		// TODO Auto-generated method stub
		return bagMapper.selectByPrimaryKey(bagId);
	}

	@Override
	public List<BisBagProject> selectByBagId(Map<String, Object> map) {
		String bagId=(String) map.get("bagId");
		BisBagProjectExample example=new BisBagProjectExample();
		com.system.common.pojo.BisBagProjectExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(bagId)) {
			criteria.andBagIdEqualTo(bagId);
		}
		List<BisBagProject> selectByExample = bagProjectMapper.selectByExample(example);
		for (BisBagProject bisBagProject : selectByExample) {
			String projectId = bisBagProject.getProjectId();
			
			BisNurseProject bisNurseProject = projectMapper.selectByPrimaryKey(projectId);
			bisBagProject.setProjectName(bisNurseProject.getProjectName());
		}
		return selectByExample;
	}

	@Override
	public List<BisNurseProject> selectByExample() {
		BisNurseProjectExample example = new BisNurseProjectExample();
		return this.projectMapper.selectByExample(example);
	
	}

	/**
	 * 添加 礼包
	 */
	@Override
	public int insert(BisNurseBag record) {
		Date putawayTime=record.getPutawayTime();
		Date soldoutTime=record.getSoldoutTime();
		if (DateUtils.compareDate(putawayTime,new Date())) {
			//待上架
			record.setStatus("0");
		}else if (DateUtils.isInDate(new Date(), putawayTime,soldoutTime)) {
			//在售
			record.setStatus("1");
		}else if (DateUtils.compareDate(new Date(),soldoutTime)) {
			//下架
			record.setStatus("2");
		}
		return bagMapper.insert(record);
	}

	/**
	 * 添加  项目与礼包的桥表
	 */
	@Override
	public int insert(BisBagProject record) {
		return bagProjectMapper.insert(record);
	}

	/**
	 * 礼包    修改
	 */
	@Override
	public int updateByPrimaryKey(BisNurseBag record) {
		Date putawayTime=record.getPutawayTime();
		Date soldoutTime=record.getSoldoutTime();
		if (DateUtils.compareDate(putawayTime,new Date())) {
			//待上架
			record.setStatus("0");
		}else if (DateUtils.isInDate(new Date(), putawayTime,soldoutTime)) {
			//在售
			record.setStatus("1");
		}else if (DateUtils.compareDate(new Date(),soldoutTime)) {
			//下架
			record.setStatus("2");
		}
		return bagMapper.updateByPrimaryKey(record);
	}

	/**
	 * 礼包  修改赋值
	 */
	@Override
	public List<BisBagProject> selectByBagId(String bagId) {
		List<BisBagProject> bisBagProjects = bagProjectMapper.selectByBagId(bagId);
		for (BisBagProject bisBagProject : bisBagProjects) {
			BisNurseProject bisNurseProject = projectMapper.selectByPrimaryKey(bisBagProject.getProjectId());
			bisBagProject.setProjectName(bisNurseProject.getProjectName());
		}
		return bisBagProjects;
	}

	/**
	 * 删除 礼包护理项目桥表信息
	 * @param bagId
	 * @return
	 */
	@Override
	public int deleteByBagId(String bagId) {
		return bagProjectMapper.delete(bagId);
	}

	@Override
	public List<BisNurseBag> select() {
		BisNurseBagExample example=new BisNurseBagExample();
		List<BisNurseBag> selectByExample = bagMapper.selectByExample(example);
		 return selectByExample;
	}
}
